package com.ibeetl.cms.web;

import com.alibaba.fastjson.JSON;
import com.ibeetl.admin.core.entity.Message;
import com.ibeetl.admin.core.service.MessageService;
import com.ibeetl.admin.core.util.AESMacUtil;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.cms.entity.*;
import com.ibeetl.cms.service.*;
import com.ibeetl.cms.util.BusinessTypeConstant;
import com.ibeetl.cms.util.ProcessConstant;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.SmartValidator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 外部接口
 * @Date 2020.4.3
 * @Author Likai
 */
@Controller
public class AppController {

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "appController";

    @Autowired
    private MessageService messageService;

    @Autowired
    private IndustrialTalentsService industrialTalentsService;

    @Autowired
    private BuyHouseSubsidiesService buyHouseSubsidiesService;

    @Autowired
    private SkillService skillService;

    @Autowired
    private ProfessionalTechnologyService professionalTechnologyService;
    @Autowired
    private RentingService rentingService;
    @Autowired
    private OutboundSubsidyService outboundSubsidyService;
    @Autowired
    private DoctoralSubsidyService doctoralSubsidyService;
    @Autowired
    private HighlevelTalentsService highlevelTalentsService;

    @Autowired
    private SmartValidator validator;

    @Autowired
    private TelentCardService telentCardService;

    @Autowired
    private InvokeLogService invokeLogService;

    @Autowired
    private RelativesService relativesService;

    @Autowired
    private EnterpriseService enterpriseService;
    /*

    涉及修改表字段的sql
        ALTER TABLE `talent_subsidies`.`industrial_talents`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

        ALTER TABLE `talent_subsidies`.`buy_house_subsidies`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

            ALTER TABLE `talent_subsidies`.`skill`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

            ALTER TABLE `talent_subsidies`.`professional_technology`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

            ALTER TABLE `talent_subsidies`.`renting`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

            ALTER TABLE `talent_subsidies`.`doctoral_subsidy`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

            ALTER TABLE `talent_subsidies`.`outbound_subsidy`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

            ALTER TABLE `talent_subsidies`.`highlevel_talents`
    ADD COLUMN `social_credit_code` varchar(255) NULL COMMENT '社会信用代码' AFTER `task_oid`,
    ADD COLUMN `external_task_id` varchar(255) NULL COMMENT '外部系统流水id' AFTER `social_credit_code`,
    ADD COLUMN `invoke_status` varchar(255) NULL COMMENT '调用状态' AFTER `external_task_id`;

        新增了一个invoke_log表

        DROP TABLE IF EXISTS `invoke_log`;
CREATE TABLE `invoke_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) DEFAULT NULL COMMENT '类型',
  `message` varchar(2550) DEFAULT NULL COMMENT '消息',
  `decode_message` varchar(2550) DEFAULT NULL COMMENT '解密消息',
  `result` varchar(255) DEFAULT NULL COMMENT '接口结果',
  `invoke_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '调用时间',
  `method` varchar(255) DEFAULT NULL COMMENT '接口方法',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=107 DEFAULT CHARSET=utf8;

        1	产业人才申报
        2	购房补贴申报
        3	技能提升奖励申报
        4	专业技术提升奖励申报
        5	租房生活补贴申报
        6	博士后在站补贴申报
        7	博士后出站补贴申报
        8	高层次人才奖励补贴

            talent_card 新加了字段，social_credit_code
            ALTER TABLE `talent_subsidies`.`telent_card`
ADD COLUMN `social_credit_code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '社会信用代码' AFTER `seq`;

ALTER TABLE `talent_subsidies`.`outbound_subsidy`
ADD COLUMN `certificate` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学位证书' AFTER `subsidy_number`;
     */



    /**
     * 申报业务接口
     * @param type 业务类型
     * @param message 业务消息
     * @return 业务结果
     */
    @PostMapping(MODEL + "/declarationInformation.json")
    @ResponseBody
    public JsonResult<String> declarationInformation(int type, String message) {
        try {

            log.info("type is " + type);
            log.info("message is " + message);
            boolean result = false;

            /**
             * 开发调试关闭了加密
             * mac上解密运行有问题
             * windows可以正常运行
             * decode message
             */
//            message = messageEncode(message);
            String originalMessage = message;

            message = messageDecode(message);


            /**
             * 对消息按照类型进行处理
             1	产业人才申报
             2	购房补贴申报
             3	技能提升奖励申报
             4	专业技术提升奖励申报
             5	租房生活补贴申报
             6	博士后在站补贴申报
             7	博士后出站补贴申报
             8	高层次人才奖励补贴
             */
            if(type == 1){
                IndustrialTalents industrialTalents = JSON.parseObject(message, IndustrialTalents.class);

                /**
                 * 进行校验
                 * 初步想法是在各实体用注解表面必填等属性
                 * 然后通过spring validator同一校验
                 */
                BindException bindException = new BindException(industrialTalents,"industrialTalents");
                try {
                    validator.validate(industrialTalents, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                industrialTalents.setCreationDate(new Date());
                industrialTalents.setDateReporting(new Date());
                industrialTalents.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                industrialTalents.setBusinessStateCode(ProcessConstant.TO_COMPANY);
                /**
                 * externalTaskId已存在就更新，否则新增
                 */
                PageQuery<IndustrialTalents> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", industrialTalents.getExternalTaskId());
                PageQuery<IndustrialTalents> industrialTalentsPageQuery = industrialTalentsService.queryByCondition(pageQuery);
                /**
                 * 根据社会信用代码获取企业userid
                 */
                industrialTalents.setCompanyId(enterpriseService.findorgUserId(industrialTalents.getSocialCreditCode()));
                List<IndustrialTalents> industrialTalentsList = industrialTalentsPageQuery.getList();
                if (industrialTalentsList.size() > 0) {
                    IndustrialTalents temp = industrialTalentsList.get(0);
                    industrialTalents.setId(temp.getId());
                    industrialTalents.setCreationDate(temp.getCreationDate());
                    industrialTalents.setDateReporting(temp.getDateReporting());
                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        industrialTalents.setBusinessState(temp.getBusinessState());
                        industrialTalents.setBusinessStateCode(temp.getBusinessStateCode());
                    }
*/
                    result = industrialTalentsService.updateTemplate(industrialTalents);
                } else {
                    result = industrialTalentsService.save(industrialTalents);
                }

            }else if(type == 2){
                BuyHouseSubsidies buyHouseSubsidies = JSON.parseObject(message, BuyHouseSubsidies.class);

                BindException bindException = new BindException(buyHouseSubsidies,"buyHouseSubsidies");
                try {
                    validator.validate(buyHouseSubsidies, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                buyHouseSubsidies.setCreationDate(new Date());
                buyHouseSubsidies.setDeclareTime(new Date());
                buyHouseSubsidies.setDateReporting(new Date());
                buyHouseSubsidies.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                buyHouseSubsidies.setBusinessStateCode(ProcessConstant.TO_COMPANY);

                PageQuery<BuyHouseSubsidies> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", buyHouseSubsidies.getExternalTaskId());
                PageQuery<BuyHouseSubsidies> buyHouseSubsidiesPageQuery = buyHouseSubsidiesService.queryByCondition(pageQuery);
                buyHouseSubsidies.setCompanyId(enterpriseService.findorgUserId(buyHouseSubsidies.getSocialCreditCode()));

                List<BuyHouseSubsidies> buyHouseSubsidiesList = buyHouseSubsidiesPageQuery.getList();
                if (buyHouseSubsidiesList.size() > 0) {
                    BuyHouseSubsidies temp = buyHouseSubsidiesList.get(0);
                    buyHouseSubsidies.setId(temp.getId());
                    buyHouseSubsidies.setCreationDate(temp.getCreationDate());
                    buyHouseSubsidies.setDateReporting(temp.getDateReporting());
                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        buyHouseSubsidies.setBusinessState(temp.getBusinessState());
                        buyHouseSubsidies.setBusinessStateCode(temp.getBusinessStateCode());
                    }
*/
                    result = buyHouseSubsidiesService.updateTemplate(buyHouseSubsidies);
                } else {
                    result = buyHouseSubsidiesService.save(buyHouseSubsidies);

                }

            }else if(type == 3){
                Skill skill = JSON.parseObject(message, Skill.class);

                BindException bindException = new BindException(skill,"skill");
                try {
                    validator.validate(skill, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                skill.setCreationDate(new Date());
                skill.setDateReporting(new Date());
                skill.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                skill.setBusinessStateCode(ProcessConstant.TO_COMPANY);
                skill.setCompanyId(enterpriseService.findorgUserId(skill.getSocialCreditCode()));

                PageQuery<Skill> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", skill.getExternalTaskId());
                PageQuery<Skill> skillPageQuery = skillService.queryByCondition(pageQuery);
                List<Skill> skillList = skillPageQuery.getList();
                if (skillList.size() > 0) {
                    Skill temp = skillList.get(0);
                    skill.setId(temp.getId());
                    skill.setCreationDate(temp.getCreationDate());
                    skill.setDateReporting(temp.getDateReporting());
                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        skill.setBusinessState(temp.getBusinessState());
                        skill.setBusinessStateCode(temp.getBusinessStateCode());
                    }*/

                    result = skillService.updateTemplate(skill);
                } else {
                    result = skillService.save(skill);

                }

            }else if(type == 4){
                ProfessionalTechnology professionalTechnology = JSON.parseObject(message, ProfessionalTechnology.class);
                BindException bindException = new BindException(professionalTechnology,"professionalTechnology");
                try {
                    validator.validate(professionalTechnology, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                professionalTechnology.setCreationDate(new Date());
                professionalTechnology.setDateReporting(new Date());
                professionalTechnology.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                professionalTechnology.setBusinessStateCode(ProcessConstant.TO_COMPANY);
                professionalTechnology.setCompanyId(enterpriseService.findorgUserId(professionalTechnology.getSocialCreditCode()));

                PageQuery<ProfessionalTechnology> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", professionalTechnology.getExternalTaskId());
                PageQuery<ProfessionalTechnology> professionalTechnologyPageQuery = professionalTechnologyService.queryByCondition(pageQuery);
                List<ProfessionalTechnology> professionalTechnologyList = professionalTechnologyPageQuery.getList();
                if (professionalTechnologyList.size() > 0) {
                    ProfessionalTechnology temp = professionalTechnologyList.get(0);
                    professionalTechnology.setId(temp.getId());
                    professionalTechnology.setCreationDate(temp.getCreationDate());
                    professionalTechnology.setDateReporting(temp.getDateReporting());
                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        professionalTechnology.setBusinessState(temp.getBusinessState());
                        professionalTechnology.setBusinessStateCode(temp.getBusinessStateCode());
                    }
*/
                    result = professionalTechnologyService.updateTemplate(professionalTechnology);
                } else {
                    result = professionalTechnologyService.save(professionalTechnology);

                }

            }else if(type == 5){
                Renting renting = JSON.parseObject(message, Renting.class);
                BindException bindException = new BindException(renting,"renting");
                try {
                    validator.validate(renting, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                renting.setCreationDate(new Date());
                renting.setDateReporting(new Date());
                renting.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                renting.setBusinessStateCode(ProcessConstant.TO_COMPANY);
                renting.setCompanyId(enterpriseService.findorgUserId(renting.getSocialCreditCode()));

                PageQuery<Renting> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", renting.getExternalTaskId());
                PageQuery<Renting> rentingPageQuery = rentingService.queryByCondition(pageQuery);
                List<Renting> rentingList = rentingPageQuery.getList();
                if (rentingList.size() > 0) {
                    Renting temp = rentingList.get(0);
                    renting.setId(temp.getId());
                    renting.setCreationDate(temp.getCreationDate());
                    renting.setDateReporting(temp.getDateReporting());
                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        renting.setBusinessState(temp.getBusinessState());
                        renting.setBusinessStateCode(temp.getBusinessStateCode());
                    }*/

                    result = rentingService.updateTemplate(renting);
                    /*if(result){
                        List<Relatives> relativesList = relativesService.findAllRelativesId(renting.getId());
                        if(relativesList != null || relativesList.size() > 0){
                            relativesService.deleteRelativesByParam(renting.getId());
                        }
                    }*/
                } else {
                    result = rentingService.save(renting);
                }
                /**
                 * 非未婚情况下 - 补充增加亲属关系
                 */
                if (result && !renting.getMaritalStatus().equals("2")) {
                    List<Relatives> relatives = renting.getRelativesList();
                    if(relatives != null){
                        //先删除
                        relativesService.deleteRelativesByParam(renting.getId());
                        for (Relatives r : relatives) {
                            r.setRelativesId(renting.getId());
                            relativesService.save(r);
                        }
                    }
                }



            }else if(type == 6){
                DoctoralSubsidy doctoralSubsidy = JSON.parseObject(message, DoctoralSubsidy.class);
                BindException bindException = new BindException(doctoralSubsidy,"doctoralSubsidy");
                try {
                    validator.validate(doctoralSubsidy, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                doctoralSubsidy.setCreationDate(new Date());
                doctoralSubsidy.setDateReporting(new Date());
                doctoralSubsidy.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                doctoralSubsidy.setBusinessStateCode(ProcessConstant.TO_COMPANY);

                doctoralSubsidy.setCompanyId(enterpriseService.findorgUserId(doctoralSubsidy.getSocialCreditCode()));

                PageQuery<DoctoralSubsidy> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", doctoralSubsidy.getExternalTaskId());
                PageQuery<DoctoralSubsidy> doctoralSubsidyPageQuery = doctoralSubsidyService.queryByCondition(pageQuery);
                List<DoctoralSubsidy> doctoralSubsidyList = doctoralSubsidyPageQuery.getList();
                if (doctoralSubsidyList.size() > 0) {
                    DoctoralSubsidy temp = doctoralSubsidyList.get(0);
                    doctoralSubsidy.setId(temp.getId());
                    doctoralSubsidy.setCreationDate(temp.getCreationDate());
                    doctoralSubsidy.setDateReporting(temp.getDateReporting());
                   /* if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        doctoralSubsidy.setBusinessState(temp.getBusinessState());
                        doctoralSubsidy.setBusinessStateCode(temp.getBusinessStateCode());
                    }*/

                    result = doctoralSubsidyService.updateTemplate(doctoralSubsidy);
                } else {
                    result = doctoralSubsidyService.save(doctoralSubsidy);
                }

            }else if(type == 7){

                OutboundSubsidy outboundSubsidy = JSON.parseObject(message, OutboundSubsidy.class);
                BindException bindException = new BindException(outboundSubsidy,"outboundSubsidy");
                try {
                    validator.validate(outboundSubsidy, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                outboundSubsidy.setCreationDate(new Date());
                outboundSubsidy.setDateReporting(new Date());
                outboundSubsidy.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                outboundSubsidy.setBusinessStateCode(ProcessConstant.TO_COMPANY);
                outboundSubsidy.setCompanyId(enterpriseService.findorgUserId(outboundSubsidy.getSocialCreditCode()));

                PageQuery<OutboundSubsidy> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", outboundSubsidy.getExternalTaskId());
                PageQuery<OutboundSubsidy> outboundSubsidyPageQuery = outboundSubsidyService.queryByCondition(pageQuery);
                List<OutboundSubsidy> outboundSubsidyList = outboundSubsidyPageQuery.getList();
                if (outboundSubsidyList.size() > 0) {
                    OutboundSubsidy temp = outboundSubsidyList.get(0);
                    outboundSubsidy.setId(temp.getId());
                    outboundSubsidy.setCreationDate(temp.getCreationDate());
                    outboundSubsidy.setDateReporting(temp.getDateReporting());

                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        outboundSubsidy.setBusinessState(temp.getBusinessState());
                        outboundSubsidy.setBusinessStateCode(temp.getBusinessStateCode());
                    }*/
                    result = outboundSubsidyService.updateTemplate(outboundSubsidy);
                } else {
                    result = outboundSubsidyService.save(outboundSubsidy);
                }



            }
            else if(type == 8){
                HighlevelTalents highlevelTalents = JSON.parseObject(message, HighlevelTalents.class);
                BindException bindException = new BindException(highlevelTalents,"highlevelTalents");
                try {
                    validator.validate(highlevelTalents, bindException);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if (bindException.hasErrors()) {
                    JsonResult<String> jsonResult = JsonResult.failMessage("validate error");
                    jsonResult.setData(bindException.getMessage());
                    return jsonResult;
                }
                /**
                 * 记录相关状态并插入数据库中
                 */
                highlevelTalents.setCreationDate(new Date());
                highlevelTalents.setDateReporting(new Date());
                highlevelTalents.setBusinessState(ProcessConstant.TO_COMPANY_STR);
                highlevelTalents.setBusinessStateCode(ProcessConstant.TO_COMPANY);
                highlevelTalents.setCompanyId(enterpriseService.findorgUserId(highlevelTalents.getSocialCreditCode()));

                PageQuery<HighlevelTalents> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", highlevelTalents.getExternalTaskId());
                PageQuery<HighlevelTalents> highlevelTalentsPageQuery = highlevelTalentsService.queryByCondition(pageQuery);
                List<HighlevelTalents> highlevelTalentsList = highlevelTalentsPageQuery.getList();
                if (highlevelTalentsList.size() > 0) {
                    HighlevelTalents temp = highlevelTalentsList.get(0);
                    highlevelTalents.setId(temp.getId());
                    highlevelTalents.setCreationDate(temp.getCreationDate());
                    highlevelTalents.setDateReporting(temp.getDateReporting());
                    /*if(temp.getBusinessState().equals(ProcessConstant.TO_FIRSTCHECK) || temp.getBusinessState().equals(ProcessConstant.DONE)){
                        //企业审核通过和复审通过的情况下 状态不在变更
                        highlevelTalents.setBusinessState(temp.getBusinessState());
                        highlevelTalents.setBusinessStateCode(temp.getBusinessStateCode());
                    }*/

                    result = highlevelTalentsService.updateTemplate(highlevelTalents);
                } else {
                    result = highlevelTalentsService.save(highlevelTalents);
                }

            }

            /**
             * 记录接口调用日志
             */
            invokeLog(type, originalMessage, result, "declarationInformation.json");

            if (result) {
                return JsonResult.success();
            } else {
                return JsonResult.failMessage("保存失败");
            }
        }catch (Exception e){
            log.error(e.getMessage());
            return JsonResult.failMessage(e.getMessage());
        }
    }

    private void invokeLog(int type, String message, Boolean result, String method) {
        InvokeLog invokeLog = new InvokeLog();
        invokeLog.setType(type);
        invokeLog.setMessage(message);
        invokeLog.setDecodeMessage(messageDecode(message));
        invokeLog.setResult(result.toString());
        invokeLog.setMethod(method);
        invokeLog.setInvokeTime(new Date());
        invokeLogService.save(invokeLog);
    }

    /**
     * @param type
     * @param message {id:xxx, result}
     * @return
     */
    @PostMapping(MODEL + "/checkResult.json")
    @ResponseBody
    public JsonResult<String> checkResult(int type, String message) {
        try {

            log.info("type is " + type);
            log.info("message is " + message);

            /**
             * decode message
             */
//            message = messageEncode(message);
            String originalMessage = message;
            message = messageDecode(message);


            CheckResult checkResult = JSON.parseObject(message,CheckResult.class);
            String id = checkResult.getId();
            int processResult = checkResult.getResult();
            String msg = checkResult.getMsg();

            /**
             * invoke log
             */
            invokeLog(type, originalMessage, true, "checkResult.json");
            /**
             * 对消息按照类型进行处理
             1	产业人才申报
             2	购房补贴申报
             3	技能提升奖励申报
             4	专业技术提升奖励申报
             5	租房生活补贴申报
             6	博士后在站补贴申报
             7	博士后出站补贴申报
             8	高层次人才奖励补贴
             */
            if(type == 1){

                /**
                 * 根据externalTaskid查出对应的记录
                 */
                PageQuery<IndustrialTalents> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<IndustrialTalents> industrialTalentsPageQuery = industrialTalentsService.queryByCondition(pageQuery);
                List<IndustrialTalents> industrialTalentsList = industrialTalentsPageQuery.getList();
                if(industrialTalentsList.size() > 0){
                    IndustrialTalents industrialTalents = industrialTalentsList.get(0);
                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(industrialTalents.getCompanyId());
                    messageObj.setUserId(industrialTalents.getUserId());
                    messageObj.setName(industrialTalents.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.industrialTalents));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        /**
                         * 修改状态并更新
                         */
                        industrialTalents.setBusinessState(ProcessConstant.DONE_STR);
                        industrialTalents.setBusinessStateCode(ProcessConstant.DONE);
                        industrialTalents.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        industrialTalents.setHandlingSuggestion(msg);
                        boolean success = industrialTalentsService.updateTemplate(industrialTalents);
                        if (success) {
                            //新业务要求：人才卡需待公示后统一生成 20200225
                            //生成产业人才卡
                            telentCardService.generateCardForApp(industrialTalents.getSalary(), industrialTalents.getTaxMonths(), industrialTalents.getName(), industrialTalents.getSex(), industrialTalents.getIdentificationNumber(),
                                    industrialTalents.getSocialCreditCode());

                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        industrialTalents.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        industrialTalents.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        industrialTalents.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        industrialTalents.setHandlingSuggestion(msg);

                        boolean success = industrialTalentsService.updateTemplate(industrialTalents);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        industrialTalents.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        industrialTalents.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        industrialTalents.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        industrialTalents.setHandlingSuggestion(msg);

                        boolean success = industrialTalentsService.updateTemplate(industrialTalents);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("人才申报找不到该记录：" + id);
                }
            }else if(type == 2){
                PageQuery<BuyHouseSubsidies> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<BuyHouseSubsidies> buyHouseSubsidiesPageQuery = buyHouseSubsidiesService.queryByCondition(pageQuery);
                List<BuyHouseSubsidies> buyHouseSubsidiesList = buyHouseSubsidiesPageQuery.getList();
                if(buyHouseSubsidiesList.size() > 0){
                    BuyHouseSubsidies buyHouseSubsidies = buyHouseSubsidiesList.get(0);
                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(buyHouseSubsidies.getCompanyId());
                    messageObj.setUserId(buyHouseSubsidies.getUserId());
                    messageObj.setName(buyHouseSubsidies.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.BUYHOUSE));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);
                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        buyHouseSubsidies.setBusinessState(ProcessConstant.DONE_STR);
                        buyHouseSubsidies.setBusinessStateCode(ProcessConstant.DONE);
                        buyHouseSubsidies.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        buyHouseSubsidies.setHandlingSuggestion(msg);

                        boolean success = buyHouseSubsidiesService.updateTemplate(buyHouseSubsidies);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        buyHouseSubsidies.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        buyHouseSubsidies.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        buyHouseSubsidies.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        buyHouseSubsidies.setHandlingSuggestion(msg);

                        boolean success = buyHouseSubsidiesService.updateTemplate(buyHouseSubsidies);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        buyHouseSubsidies.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        buyHouseSubsidies.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        buyHouseSubsidies.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        buyHouseSubsidies.setHandlingSuggestion(msg);

                        boolean success = buyHouseSubsidiesService.updateTemplate(buyHouseSubsidies);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("购房补贴找不到该记录：" + id);
                }

            }else if(type == 3){
                PageQuery<Skill> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<Skill> skillPageQuery = skillService.queryByCondition(pageQuery);
                List<Skill> skillList = skillPageQuery.getList();
                if(skillList.size() > 0){
                    Skill skill = skillList.get(0);

                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(skill.getCompanyId());
                    messageObj.setUserId(skill.getUserId());
                    messageObj.setName(skill.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.skill));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        skill.setBusinessState(ProcessConstant.DONE_STR);
                        skill.setBusinessStateCode(ProcessConstant.DONE);
                        skill.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        skill.setHandlingSuggestion(msg);

                        boolean success = skillService.updateTemplate(skill);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        skill.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        skill.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        skill.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        skill.setHandlingSuggestion(msg);

                        boolean success = skillService.updateTemplate(skill);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        skill.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        skill.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        skill.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        skill.setHandlingSuggestion(msg);

                        boolean success = skillService.updateTemplate(skill);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("技能提升找不到该记录：" + id);
                }

            }else if(type == 4){
                PageQuery<ProfessionalTechnology> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<ProfessionalTechnology> professionalTechnologyPageQuery = professionalTechnologyService.queryByCondition(pageQuery);
                List<ProfessionalTechnology> professionalTechnologyList = professionalTechnologyPageQuery.getList();
                if(professionalTechnologyList.size() > 0){
                    ProfessionalTechnology professionalTechnology = professionalTechnologyList.get(0);
                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(professionalTechnology.getCompanyId());
                    messageObj.setUserId(professionalTechnology.getUserId());
                    messageObj.setName(professionalTechnology.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.professionalTechnology));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        professionalTechnology.setBusinessState(ProcessConstant.DONE_STR);
                        professionalTechnology.setBusinessStateCode(ProcessConstant.DONE);
                        professionalTechnology.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        professionalTechnology.setHandlingSuggestion(msg);

                        boolean success = professionalTechnologyService.updateTemplate(professionalTechnology);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        professionalTechnology.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        professionalTechnology.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        professionalTechnology.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        professionalTechnology.setHandlingSuggestion(msg);

                        boolean success = professionalTechnologyService.updateTemplate(professionalTechnology);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        professionalTechnology.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        professionalTechnology.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        professionalTechnology.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        professionalTechnology.setHandlingSuggestion(msg);

                        boolean success = professionalTechnologyService.updateTemplate(professionalTechnology);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("专业技术找不到该记录：" + id);
                }


            }else if(type == 5){
                PageQuery<Renting> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<Renting> rentingPageQuery = rentingService.queryByCondition(pageQuery);
                List<Renting> rentingList = rentingPageQuery.getList();
                if(rentingList.size() > 0){
                    Renting renting = rentingList.get(0);

                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(renting.getCompanyId());
                    messageObj.setUserId(renting.getUserId());
                    messageObj.setName(renting.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.renting));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        renting.setBusinessState(ProcessConstant.DONE_STR);
                        renting.setBusinessStateCode(ProcessConstant.DONE);
                        renting.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        renting.setHandlingSuggestion(msg);

                        boolean success = rentingService.updateTemplate(renting);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        renting.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        renting.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        renting.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        renting.setHandlingSuggestion(msg);

                        boolean success = rentingService.updateTemplate(renting);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        renting.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        renting.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        renting.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        renting.setHandlingSuggestion(msg);

                        boolean success = rentingService.updateTemplate(renting);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }

                }else{
                    return JsonResult.failMessage("租房找不到该记录：" + id);
                }

            }else if(type == 6){
                /**
                 * terminate 2
                 * back 1
                 * pass 0
                 */
                PageQuery<DoctoralSubsidy> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<DoctoralSubsidy> doctoralSubsidyPageQuery = doctoralSubsidyService.queryByCondition(pageQuery);
                List<DoctoralSubsidy> doctoralSubsidyList = doctoralSubsidyPageQuery.getList();
                if(doctoralSubsidyList.size() > 0){
                    DoctoralSubsidy doctoralSubsidy = doctoralSubsidyList.get(0);

                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(doctoralSubsidy.getCompanyId());
                    messageObj.setUserId(doctoralSubsidy.getUserId());
                    messageObj.setName(doctoralSubsidy.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.doctoralSubsidy));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        doctoralSubsidy.setBusinessState(ProcessConstant.DONE_STR);
                        doctoralSubsidy.setBusinessStateCode(ProcessConstant.DONE);
                        doctoralSubsidy.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        doctoralSubsidy.setHandlingSuggestion(msg);

                        boolean success = doctoralSubsidyService.updateTemplate(doctoralSubsidy);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        doctoralSubsidy.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        doctoralSubsidy.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        doctoralSubsidy.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        doctoralSubsidy.setHandlingSuggestion(msg);

                        boolean success = doctoralSubsidyService.updateTemplate(doctoralSubsidy);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        doctoralSubsidy.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        doctoralSubsidy.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        doctoralSubsidy.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        doctoralSubsidy.setHandlingSuggestion(msg);

                        boolean success = doctoralSubsidyService.updateTemplate(doctoralSubsidy);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("博士后入站找不到该记录：" + id);
                }
            }else if(type == 7){
                /**
                 * terminate 2
                 * back 1
                 * pass 0
                 */
                PageQuery<OutboundSubsidy> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<OutboundSubsidy> outboundSubsidyPageQuery = outboundSubsidyService.queryByCondition(pageQuery);
                List<OutboundSubsidy> outboundSubsidyList = outboundSubsidyPageQuery.getList();
                if(outboundSubsidyList.size() > 0){
                    OutboundSubsidy outboundSubsidy = outboundSubsidyList.get(0);

                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(outboundSubsidy.getCompanyId());
                    messageObj.setUserId(outboundSubsidy.getUserId());
                    messageObj.setName(outboundSubsidy.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.outboundSubsidy));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        outboundSubsidy.setBusinessState(ProcessConstant.DONE_STR);
                        outboundSubsidy.setBusinessStateCode(ProcessConstant.DONE);
                        outboundSubsidy.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        outboundSubsidy.setHandlingSuggestion(msg);

                        boolean success = outboundSubsidyService.updateTemplate(outboundSubsidy);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        outboundSubsidy.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        outboundSubsidy.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        outboundSubsidy.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        outboundSubsidy.setHandlingSuggestion(msg);

                        boolean success = outboundSubsidyService.updateTemplate(outboundSubsidy);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        outboundSubsidy.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        outboundSubsidy.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        outboundSubsidy.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        outboundSubsidy.setHandlingSuggestion(msg);

                        boolean success = outboundSubsidyService.updateTemplate(outboundSubsidy);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("博士后出战找不到该记录：" + id);
                }
            } else if(type == 8){

                PageQuery<HighlevelTalents> pageQuery = new PageQuery<>();
                pageQuery.setPara("externalTaskId", id);
                PageQuery<HighlevelTalents> highlevelTalentsPageQuery = highlevelTalentsService.queryByCondition(pageQuery);
                List<HighlevelTalents> highlevelTalentsList = highlevelTalentsPageQuery.getList();
                if(highlevelTalentsList.size() > 0){
                    HighlevelTalents highlevelTalents = highlevelTalentsList.get(0);
                    //给企业发一条信息
                    Message messageObj = new Message();
                    messageObj.setCompanyId(highlevelTalents.getCompanyId());
                    messageObj.setUserId(highlevelTalents.getUserId());
                    messageObj.setName(highlevelTalents.getName());
                    messageObj.setStatus(-1);
                    messageObj.setContent(msg);
                    messageObj.setType(Integer.parseInt(BusinessTypeConstant.highlevelTalents));
                    messageObj.setCreateDate(new Date());
                    messageService.save(messageObj);

                    /**
                     * 审核结果processResult各种值
                     * 0 通过
                     * 1 退回
                     * 2 终止
                     */
                    if(processResult == 0){
                        highlevelTalents.setBusinessState(ProcessConstant.DONE_STR);
                        highlevelTalents.setBusinessStateCode(ProcessConstant.DONE);
                        highlevelTalents.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                        highlevelTalents.setHandlingSuggestion(msg);

                        boolean success = highlevelTalentsService.updateTemplate(highlevelTalents);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 1){
                        highlevelTalents.setBusinessState(ProcessConstant.WAIT_APPLICATION_STR);
                        highlevelTalents.setBusinessStateCode(ProcessConstant.WAIT_APPLICATION);
                        highlevelTalents.setProcessResult(ProcessConstant.RETURN_BACK_STR);
                        highlevelTalents.setHandlingSuggestion(msg);

                        boolean success = highlevelTalentsService.updateTemplate(highlevelTalents);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }else if(processResult == 2){
                        highlevelTalents.setBusinessState(ProcessConstant.TERMINIATE_STR);
                        highlevelTalents.setBusinessStateCode(ProcessConstant.TERMINIATE);
                        highlevelTalents.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
                        highlevelTalents.setHandlingSuggestion(msg);

                        boolean success = highlevelTalentsService.updateTemplate(highlevelTalents);
                        if (success) {
                            return JsonResult.success();
                        } else {
                            return JsonResult.failMessage("保存失败");
                        }
                    }
                }else{
                    return JsonResult.failMessage("高层次人才找不到该记录：" + id);
                }

            }
            return JsonResult.failMessage("参数错误");
        }catch (Exception e){
            log.error(e.getMessage());
            return JsonResult.failMessage(e.getMessage());
        }
    }

    /**
     * 在mac上会报错
     * windows不会
     * @param message
     * @return
     */
    private final String PWD = "mc-talentLicense";
    private String messageDecode(String message) {
        return AESMacUtil.decrypt(message, PWD);
    }

    private String messageEncode(String message) {
        return AESMacUtil.encrypt(message, PWD);
    }

    @Autowired
    private PretrialService pretrialService;

    /**
     * @return 业务结果
     */
    @PostMapping(MODEL + "/postTest.json")
    @ResponseBody
    public JsonResult<String> postTest() {
        pretrialService.post(1, "GRFC20200400184", "0", "", "元", "同意", "1");
        return JsonResult.success();
    }

    /**
     * 根据身份证获取人才卡
     *
     * @param idCard
     * @return
     */
    @PostMapping(MODEL + "/TalentCard.json")
    @ResponseBody
    public JsonResult<TelentCard> TalentCard(String idCard, String license) {
        if ("mc-license".equalsIgnoreCase(license)) {
            TelentCard card = telentCardService.findCardForApp(idCard);
            if (card == null) {
                return JsonResult.successMessage("查无此人");
            }
            return JsonResult.success(card);
        } else {
            return JsonResult.failMessage("错误的通行证");
        }

    }


    public static void main(String args[]) {
        Renting renting = new Renting();
        renting.setProcessResult("ttttt");
        List<Relatives> relativesList = new ArrayList<>();
        Relatives relatives = new Relatives();
        relatives.setRelationship("fumu");

        Relatives r2 = new Relatives();
        r2.setRelationship("mmmmm");
        relativesList.add(relatives);
        relativesList.add(r2);
        renting.setRelativesList(relativesList);

        String str = JSON.toJSONString(renting);
        System.out.println(str);
        Renting renting1 = JSON.parseObject(str, Renting.class);
        System.out.println(renting1);

    }
}
